package com.crs.controller.user;

import com.crs.dto.UserLoginDTO;
import com.crs.dto.UserRegisterDTO;
import com.crs.result.Result;
import com.crs.service.UserService;
import com.crs.vo.UserLoginVo;
import com.crs.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;

@RestController
@Slf4j
@RequestMapping("/user")
public class userController {

    @Autowired
    private UserService userService;
    
    @Autowired
    private AliOssUtil aliOssUtil;

    @PostMapping("/login")
    public Result<UserLoginVo> login(@RequestBody @Validated UserLoginDTO userLoginDTO) {
        log.info("用户登录:"+userLoginDTO);
        UserLoginVo userLoginVo = userService.login(userLoginDTO);
        return Result.success(userLoginVo);
    }

    @PostMapping("/register")
    public Result register(@RequestPart("avatar") MultipartFile avatar, @RequestPart("userRegisterDTO") @Validated UserRegisterDTO userRegisterDTO) {
        log.info("用户注册: {}, 头像文件: {}", userRegisterDTO, avatar.getOriginalFilename());
        try {
            // 执行注册
            userService.register(userRegisterDTO, avatar);
        return Result.success();
        } catch (Exception e) {
            log.error("头像上传失败", e);
            return Result.error("头像上传失败");
        }
    }

    //TODO 忘记密码功能
}
